package com.lottery.ruok.lottrey.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChuantongcaiActivity;
import com.lottery.ruok.lottrey.activity.LiuhecaiDetailActivity;
import com.lottery.ruok.lottrey.activity.OfficalLotteryDetailActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/2.
 */

public class PlayDialog extends DialogFragment {
    @BindView(R.id.numberDistribute)
    TextView numberDistribute;
    @BindView(R.id.recentPrize)
    TextView recentPrize;
    @BindView(R.id.buyLog)
    TextView buyLog;
    @BindView(R.id.playTip)
    TextView playTip;
    @BindView(R.id.lotteryRule)
    TextView lotteryRule;
    @BindView(R.id.whole_bg)
    LinearLayout whole_bg;

    private OfficalLotteryDetailActivity newLotteryDetailActivity;
    private ChuantongcaiActivity chuantongcaiActivity;
    private LiuhecaiDetailActivity liuhecaiDetailActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //自定义对话框样式
        View v = inflater.inflate(R.layout.dialog_play_layout, null);

        ButterKnife.bind(this, v);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.RIGHT | Gravity.TOP;
        lp.y = Utils.dip2px(getContext().getApplicationContext(), 20);

        window.setAttributes(lp);

        getDialog().setCanceledOnTouchOutside(true);
        if(liuhecaiDetailActivity != null){
            numberDistribute.setText("号码走势");
        }

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if(activity instanceof OfficalLotteryDetailActivity) {
            newLotteryDetailActivity = (OfficalLotteryDetailActivity) activity;
        } else if(activity instanceof ChuantongcaiActivity){
            chuantongcaiActivity = (ChuantongcaiActivity) activity;
        } else if(activity instanceof LiuhecaiDetailActivity){
            liuhecaiDetailActivity = (LiuhecaiDetailActivity) activity;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        Window window = getDialog().getWindow();
        window.setLayout(Utils.dip2px(getActivity(), 100), Utils.dip2px(getActivity(), 200));

        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        window.setAttributes(windowParams);
    }

    @OnClick({R.id.numberDistribute, R.id.recentPrize, R.id.buyLog, R.id.playTip, R.id.lotteryRule, R.id.whole_bg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.numberDistribute:
                if(newLotteryDetailActivity != null) {
                    newLotteryDetailActivity.gotoPrizeList();
                }
                if(chuantongcaiActivity != null) {
                    chuantongcaiActivity.gotoPrizeList();
                }
                if(liuhecaiDetailActivity != null) {
                    liuhecaiDetailActivity.gotoPrizeList();
                }
                break;
            case R.id.recentPrize:
                if(newLotteryDetailActivity != null) {
//                    newLotteryDetailActivity.//sendAnalyticMsg("lottery_" + newLotteryDetailActivity.ANALYTIC_VERSION + "_" + newLotteryDetailActivity.getIntent().getStringExtra("code") + "_itemdetail_recent_draw_jump");
                    newLotteryDetailActivity.recentPrize();
                }
                if(chuantongcaiActivity != null) {
//                    chuantongcaiActivity.//sendAnalyticMsg("lottery_" + chuantongcaiActivity.ANALYTIC_VERSION + "_" + chuantongcaiActivity.getIntent().getStringExtra("code") + "_itemdetail_recent_draw_jump");
                    chuantongcaiActivity.recentPrize();
                }
                if(liuhecaiDetailActivity != null) {
//                    liuhecaiDetailActivity.//sendAnalyticMsg("lottery_" + liuhecaiDetailActivity.ANALYTIC_VERSION + "_" + liuhecaiDetailActivity.getIntent().getStringExtra("code") + "_itemdetail_recent_draw_jump");
                    liuhecaiDetailActivity.recentPrize();
                }
                break;
            case R.id.buyLog:
                if(newLotteryDetailActivity != null) {
                    newLotteryDetailActivity.gotoOrderList();
                }
                if(chuantongcaiActivity != null) {
                    chuantongcaiActivity.gotoOrderList();
                }
                if(liuhecaiDetailActivity != null) {
                    liuhecaiDetailActivity.gotoOrderList();
                }
                break;
            case R.id.playTip:
                if(newLotteryDetailActivity != null) {
                    newLotteryDetailActivity.gotoPlayTip();
                }
                if(chuantongcaiActivity != null) {
                    chuantongcaiActivity.gotoPlayTip();
                }
                if(liuhecaiDetailActivity != null) {
                    liuhecaiDetailActivity.gotoPlayTip();
                }
                break;
            case R.id.lotteryRule:
                if(newLotteryDetailActivity != null) {
                    newLotteryDetailActivity.gotoRule();
                }
                if(chuantongcaiActivity != null) {
                    chuantongcaiActivity.gotoRule();
                }
                if(liuhecaiDetailActivity != null) {
                    liuhecaiDetailActivity.gotoRule();
                }
                break;
            case R.id.whole_bg:
                dismissAllowingStateLoss();
                break;
        }
        dismissAllowingStateLoss();
    }
}
